package com.coinworld.admin.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by ZhangRuiFeng on 2019/5/11.
 */

//@WebFilter(filterName = "MSecurity",urlPatterns = {"/index","/product/*","/productOrder/*","/user/*","/community/*","/popularRouteOrder/*","/popularRoute/*"})
public class LoginStatusFilter  implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response= (HttpServletResponse) servletResponse;
        System.out.println(request.getRequestURI());
        System.out.println(request.getContextPath()+"======");
        //检查是否是登录页面
//        if(request.getRequestURI().equals("/account/login")) {
//            filterChain.doFilter(servletRequest, servletResponse);
//        }
//        if(request.getRequestURI().equals("/account/doLogin")) {
//            filterChain.doFilter(servletRequest, servletResponse);
//        }
        //检测用户是否登录
        HttpSession session =request.getSession();
        String status= (String) session.getAttribute("loginStatus");
        if(status==null || !status.equals("success"))
        {
            try{
//                response.sendRedirect(request.getContextPath()+"/account/login.html");
                java.io.PrintWriter out = response.getWriter();
                out.println("<html>");
                out.println("<script>");
                out.println("window.open ('/account/login.html','_top')");
                out.println("</script>");
                out.println("</html>");

                return;
            }catch (Exception e){}
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}